<?php
namespace modules\auth;
use modules\user\storage as user;

class storage
{
    static function schemas(): array
    {
        return [];  // @users/user
    }

    static function add_admin($q, string $username, string $password,
        string $name, string $comment, int $org)
    {
        return user::add($q, $username, $password,
            'admin', $name, $comment, $org);
    }

    static function info_from_username($q, string $username, string $codename)
    {
        return $q->table('users')
            ->where(['username', $username])
            ->where(['org.codename', $codename])
            ->field('id',
                    'credential',
                    'role',
                    'seq',
                    'org',
                    'org.codename',
                    'org.name orgname',
                    'name',
                    'active')
            ->select(0);
    }

    static function info_from_seq($q, string $seq)
    {
        return $q->table('users')
            ->where(['seq', $seq])
            ->field('id',
                    'credential',
                    'role',
                    'seq',
                    'org',
                    'org.codename',
                    'org.name orgname',
                    'name',
                    'active')
            ->select(0);
    }

    static function id_from_seq($q, string $seq)
    {
        return $q->table('users')
            ->where(['seq', $seq])
            ->field('id')
            ->select(0);
    }

    static function revoke_token($q, int $org, int $id)
    {
        return $q->table('users', $id)
            ->where(['org', $org])
            ->field('seq')
            ->update(security::random_id());
    }
}

